package com.stats.ultimate;

import java.util.ArrayList;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;

public class Create extends Activity {
	// Declare variable to hold database helper and sqlite database
	SQLiteDatabase db;
	DatabaseHelper db_helper;
	
	// Declare array to hold TextViews
	ArrayList<TextView> prompts = new ArrayList<TextView>();
	
	// Declare array to hold EditTexts
	ArrayList<EditText> players = new ArrayList<EditText>();
	
	// Declare EditText to hold teams name
	EditText team_in;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create);
        
        // Instantiate new database helper
        db_helper = new DatabaseHelper(getApplicationContext());
        
        // Instantiate save button
        Button save = (Button) findViewById(R.id.save);
        
        // Instantiate add_player button
        Button add = (Button) findViewById(R.id.add_player);
        
        // Instantiate team_in EditText field
        team_in = (EditText) findViewById(R.id.team_in);
        
        // Instantiate the relative layout inside of the scroll view
        final RelativeLayout l1 = (RelativeLayout) findViewById(R.id.relativeLayout1);
        
        // Create first EditText
        EditText first_in = new EditText(this);
        
        // Set the width of first_in to be 150
        first_in.setWidth(150);
        
        // Set the input type of first_in to be a person's name
        first_in.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
        
        // Declare parameters to be used for first_in
        RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
        		RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        
        // Set first_in to be right aligned in parent
        params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
        
        // Set first_in to be top aligned in parent
        params2.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
        
        // Set bottom margin of first_in to be 5 and right margin to be 10
        params2.setMargins(0, 0, 10, 5);
        
        // Set the parameters for first_in
        first_in.setLayoutParams(params2);
        
        // Set the id of first_in to be 1
        first_in.setId(1);
        
        // Add first_in to l1
        l1.addView(first_in);
        
        // Add first_in to the layout of players
        players.add(first_in);
        
        // Create a new TextView
        TextView first_prompt = new TextView(this);
        
        // Set the size of the text for first_prompt
        first_prompt.setTextSize(15);
        
        // Set the string for first_prompt
        first_prompt.setText("Player 1's Name");
        
        // Define the parameters to be used for this TextView
        RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
        		RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        
        // Set first prompt to be aligned left in parent
        params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
        
        // Set first_prompt to be bottom aligned with first_in
        params1.addRule(RelativeLayout.ALIGN_BOTTOM, first_in.getId());
        
        // Set the left margin of first_prompt to be 10
        params1.setMargins(10, 0, 0, 0);
        
        // Set the parameters of first_prompt
        first_prompt.setLayoutParams(params1);
        
        // Set the id of first_prompt to be 101
        first_prompt.setId(101);
        
        // Add first_prompt to l1
        l1.addView(first_prompt);
        
        // Add first_prompt to array of prompts
        prompts.add(first_prompt);
        
        // for loop to add next 6 prompts and input fields
        for(int i = 1; i < 7; i++){
        	// Create an EditText field
        	EditText temp_in = new EditText(this);
        	
        	// Set the width of this field to be 100
        	temp_in.setWidth(150);
        	
        	// Set the input type of this field to be a persons name
        	temp_in.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
        	
        	// Declare the parameters to be used for this field
        	RelativeLayout.LayoutParams temp_params2 = new RelativeLayout.LayoutParams(
        			RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        	
        	// Set this field to be aligned right with the field above it
        	temp_params2.addRule(RelativeLayout.ALIGN_RIGHT, players.get(i-1).getId());
        	
        	// Set this field to aligned below the field above it
        	temp_params2.addRule(RelativeLayout.BELOW, players.get(i-1).getId());
        	
        	// Set the bottom margin of this field to be 5
        	temp_params2.bottomMargin = 5;
        	
        	// Set the parameters for this field
        	temp_in.setLayoutParams(temp_params2);
        	
        	// Set the id of this field
        	temp_in.setId(i+1);
        	
        	// Add this field to l1
        	l1.addView(temp_in);
        	
        	// Add this field to the players array
        	players.add(temp_in);
        	
        	// Create a new TextView
        	TextView temp_prompt = new TextView(this);
        	
        	// Set the size of the text for this prompt
        	temp_prompt.setTextSize(15);
        	
        	// Format a string for this prompt
        	String temp_text = String.format("Player %d's Name", i+1);
        	
        	// Set text of this prompt
        	temp_prompt.setText(temp_text);
        	
        	// Define the parameters to be used for this TextView
        	RelativeLayout.LayoutParams temp_params1 = new RelativeLayout.LayoutParams(
        			RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        	
        	// Set the bottom margin to 5dp for this prompt
        	temp_params1.bottomMargin = 5;
        	
        	// Set this prompt to be aligned to the left with the prompt above it
        	temp_params1.addRule(RelativeLayout.ALIGN_LEFT, prompts.get(i-1).getId());
        	
        	// Set this prompt to be bottom aligned with its corresponding EditText field
        	temp_params1.addRule(RelativeLayout.ALIGN_BOTTOM, temp_in.getId());

        	// Set the paramters for this prompt
        	temp_prompt.setLayoutParams(temp_params1);
        	
        	// Set the id of this prompt
        	temp_prompt.setId(100+i+1);
        	
        	// Add this prompt to l1
        	l1.addView(temp_prompt);
        	
        	// Add this prompt to the array of prompts
        	prompts.add(temp_prompt);
        }
        
        // Set onClickListener for add
        add.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Create an EditText field
	        	EditText temp_in = new EditText(getApplicationContext());
	        	
	        	// Set the width of this field to be 150
	        	temp_in.setWidth(150);
	        	
	        	// Set the input type of this field to be a person's name
	        	temp_in.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
	        	
	        	// Declare the parameters to be used for this field
	        	RelativeLayout.LayoutParams temp_params2 = new RelativeLayout.LayoutParams(
	        			RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
	        	
	        	// Set this field to be aligned right with the field above it
	        	temp_params2.addRule(RelativeLayout.ALIGN_RIGHT, players.get(players.size()-1).getId());
	        	
	        	// Set this field to aligned below the field above it
	        	temp_params2.addRule(RelativeLayout.BELOW, players.get(players.size()-1).getId());
	        	
	        	// Set the bottom margin of this field to be 5
	        	temp_params2.bottomMargin = 5;
	        	
	        	// Set the parameters for this field
	        	temp_in.setLayoutParams(temp_params2);
	        	
	        	// Set the id of this field
	        	temp_in.setId(players.size()+1);
	        	
	        	// Add this field to l1
	        	l1.addView(temp_in);
	        	
	        	// Add this field to the players array
	        	players.add(temp_in);
	        	
	        	// Create a new TextView
	        	TextView temp_prompt = new TextView(getApplicationContext());
	        	
	        	// Set the size of the text for this prompt
	        	temp_prompt.setTextSize(15);
	        	
	        	// Format a string for this prompt
	        	String temp_text = String.format("Player %d's Name", prompts.size()+1);
	        	
	        	// Set text of this prompt
	        	temp_prompt.setText(temp_text);
	        	
	        	// Define the parameters to be used for this TextView
	        	RelativeLayout.LayoutParams temp_params1 = new RelativeLayout.LayoutParams(
	        			RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
	        	
	        	// Set the bottom margin to 5dp for this prompt
	        	temp_params1.bottomMargin = 5;
	        	
	        	// Set this prompt to be aligned to the left with the prompt above it
	        	temp_params1.addRule(RelativeLayout.ALIGN_LEFT, prompts.get(prompts.size()-1).getId());
	        	
	        	// Set this prompt to be bottom aligned with its corresponding EditText field
	        	temp_params1.addRule(RelativeLayout.ALIGN_BOTTOM, temp_in.getId());
	        	
	        	// Set the paramters for this prompt
	        	temp_prompt.setLayoutParams(temp_params1);
	        	
	        	// Set the id of this prompt
	        	temp_prompt.setId(100+prompts.size()+1);
	        	
	        	// Add this prompt to l1
	        	l1.addView(temp_prompt);
	        	
	        	// Add this prompt to the array of prompts
	        	prompts.add(temp_prompt);
			}
		});
		
        // Set onClickListener for save
        save.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Get team's name from team_in
				String team = team_in.getText().toString();
				
				// Define array to hold players' names
				ArrayList<String> names = new ArrayList<String>();
				
				// for loop to get all players names
				for(int i = 0; i < players.size(); i++){
					// Get this players name and save it to names array
					names.add(players.get(i).getText().toString());
				}
				
				// Check if given team_in is blank
				if(team.equalsIgnoreCase("")){
					// If so give message and don't create team
					Toast.makeText(getApplicationContext(), "Team name cannot be blank", Toast.LENGTH_SHORT).show();
				}
				
				// Else continue
				else{
					// Initialize blank to false
					boolean blank = false;
					
					// for loop to check all players names
					for(int i = 0; i < names.size(); i++){
						// Check if this players name is blank
						if(names.get(i).equalsIgnoreCase("")){
							// If so set blank to true and break this loop
							blank = true;
							break;
						}
					}
					
					// Check if blank is now true
					if(blank){
						// If so give a message and do not create team
						Toast.makeText(getApplicationContext(), "Players' names cannot be blank" , Toast.LENGTH_SHORT).show();
					}
					
					// Else continue
					else{
						// Initialize identical to false
						boolean identical = false;
						
						// for loop to increment through all players names but last one
						for(int i = 0; i < names.size() - 1; i++){
							// for loop to increment through all players' names after this one
							for(int j = i+1; j < names.size(); j++){
								// Check if these two players' names are identical
								if(names.get(i).equalsIgnoreCase(names.get(j))){
									// If so give message, set identical to true and break for loop
									Toast.makeText(getApplicationContext(), "Cannot create team with identical player names", Toast.LENGTH_SHORT).show();
									identical = true;
									break;
								}
							}
							
							// Check if identical is true
							if(identical){
								// If so break outer for loop as well
								break;
							}
						}
						
						// Check if identical is still false
						if(!identical){
							// If so open database for reading and writing
							db = db_helper.getWritableDatabase();

							// Call create team function with given team name and players' names
							if(db_helper.addTeam(db, team, names) < 0){
								// If unsuccessful give a notification of failure
								Toast.makeText(getApplicationContext(), "Failed to create team", Toast.LENGTH_SHORT).show();
							
								// Close the database
								db.close();
							}

							// Else team was successfully created
							else{
								// Close database
								db.close();
							
								// Finish this activity
								finish();
							}
						}
					}
				}
			}
		});
    }
}
